﻿/**

 @Name：layuiAdmin 用户管理，增删改查
 @Author：马达
 @Site：http://www.sunplus.wang
 @License: 杭州九宸智能科技有限公司
    
 */

layui.define(['table', 'form', 'laydate', 'upload', 'admin'], function (exports) {
    var $ = layui.$
    , admin = layui.admin
    , view = layui.view
    , table = layui.table
    , upload = layui.upload
    , laydate = layui.laydate
    , form = layui.form;

    var nowdate = new Date();
    var varButtonRight = "";
    //文章管理
    table.render({
        elem: '#LAY-app-content-list'
        , toolbar: true  //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
        , defaultToolbar: ['filter']
        , method: "post"
        , url: layui.setter.APIUrl + '/shop/goods_baseMgr/listPain'
        , headers: { //通过 request 头传递
            Authorization: "bearer " + layui.data('layuiAdmin').access_token
        }
        , parseData: function (res) { //res 即为原始返回的数据 
            varButtonRight = res.Data.ButtonRight;
            return {
                "ResultCode": res.ResultCode, //解析接口状态
                "ResultInfo": res.ResultInfo, //解析提示文本
                "count": res.Data.TotalRecordCount, //解析数据长度
                "Data": res.Data.Items //解析数据列表 
            };
        }
    , cols: [[
      { type: 'checkbox', fixed: 'left', field: 'PID' }, //sort: true  //hide: true //  width:100        
		//{
		//    field: 'Code', title: '商品编码/货号', width: 130, templet: function (d) {
		//        var html = "编码:<span class='font1'>" + d.Code + "</span>";
		//        html += "<br />货号:<span class='font1'>" + d.ArtNo + "</span>";
		//        return html;
		//    }
		//},
		{
		    field: 'Code', title: '商品编码', minWidth: 100, templet: function (d) {
		        var html = "<span class='fontview' lay-event=\"view\" title='点击查看商品详情'>" + d.Code + "</span>";
		        if (d.Other1 != null && d.Other1 != "") {
		            html += "<i class=\"layui-icon icon1\" id='syncnote" + d.Code + "' title='自动同步店铺商品失败' onclick=\"LookSyncNote('" + d.Other1 + "', '" + d.Code + "')\">&#xe60b;</i>";
		        }
		        return html;
		    }
		},
		{ field: 'ArtNo', title: '货号', minWidth: 100 },
		{ field: 'GoodsCategoryID_SelSourceName', title: '分类 <span class="orders" onclick="orders(3)" title="分类名称排序">⬇</span>', minWidth: 80 },
		{
		    field: 'Name', title: '名称<br/>副标题', minWidth: 320, templet: function (d) {
		        if (d.Title == null) d.Title = "";
		        var html = "<span class='font2'>" + d.Name + "</span>";
		        html += "<br /><span class='font3'>" + d.Title + "</span>";
		        return html;
		    }
		},
		{
		    field: 'Path_Upload', title: '封面', templet: function (d) {
		        var html = "<img src=" + layui.setter.ImgPath + d.Path_Upload + " alt='' />";
		        return html;
		    }, width: 90
		},
		{
		    field: 'SpecMode', title: '规格<br/>重量(kg)', minWidth: 100, templet: function (d) {
		        if (d.Title == null) d.Title = "";
		        var html = "规格：<span class='font1'>" + d.SpecMode + "</span>";
		        html += "<br />重量：<span class='font1'>" + d.Weight + "</span>";
		        return html;
		    }
            },                                     
		//{
		//    field: 'DeliveMode', title: '配送方式<br/>是否包邮', minWidth: 100, templet: function (d) {
		//        var html = "<div>";
		//        var str = "";
		//        var ship = "";
		//        if (d.DeliveMode.indexOf("0") >= 0) {
		//            str = "自提";
		//        }
		//        if (d.DeliveMode.indexOf("1") >= 0) {
		//            if (str != "") str += "、";
		//            str += "快递";
		//            if (d.IsShipping_Sel == 1) {
		//                ship += "<span style='color:#5FB878;font-weight:bold'>包邮</span>";
		//            }
		//        }
		//        html += "<span style='color:#01AAED;font-weight:bold'>" + str + "</span>";
		//        if (ship != "") {
		//            html += "<br />" + ship;
		//        }
		//        html += "</div>";
		//        return html;
		//    }
		//},
		{
		    field: 'Price', title: '价格(元)<br /><span class="orders" onclick="orders(1)" title="价格降序">⬇</span> <span class="orders" onclick="orders(2)" title="价格升序">⬆</span>', width: 90,
		    templet: function (d) {
		        var html = "<span style='color:#FF5722;font-weight:bold;'>￥</span><span style='color:#FF5722;font-weight:bold;font-size:1.1em'>" + d.Price + "</span>";
		        if (d.DisLables != "" && d.DisLables != null) {
		            var str = encodeURI(d.DisLables);
		            html += "<br /><span class='jgsp' id='jg" + d.Code + "' onclick=\"LookMemberPrice('" + str + "', '" + d.Code + "')\">....</span>";
		        }
		        return html;
		    }
		},
		{
		    field: 'Price1', title: '结算价(元)<br /><span class="orders" onclick="orders(4)" title="价格降序">⬇</span> <span class="orders" onclick="orders(5)" title="价格升序">⬆</span>', width: 90,
		    templet: function (d) {
		        if (d.Price1 == null) return "";
		        var html = "<span style='color:#FF5722;font-weight:bold;'>￥</span><span style='color:#FF5722;font-weight:bold;font-size:1.1em'>" + d.Price1 + "</span>";
		        return html;
		    }
		},
		//{
		//    field: 'Stock', title: '库存', width: 70, templet: function (d) {
		//        var html = "<span style='font-weight:bold;font-size:1.1em'>" + d.Stock + "</span>";
		//        return html;
		//    }
		//},
		//{
		//    field: 'LimitNum', title: '限购数量', width: 80, templet: function (d) {
		//        if (d.LimitNum <= 0) d.LimitNum = "无";
		//        var html = "<span style='font-weight:bold;font-size:1.1em'>" + d.LimitNum + "</span>";
		//        if (d.LimitArea != null && d.LimitArea != "") {
		//            var str = encodeURI(d.LimitArea);
		//            html += "<br /><span class='jgsp' id='area" + d.Code + "' onclick=\"LookLimitArea('" + str + "', '" + d.Code + "')\">....</span>";
		//        }
		//        return html;
		//    }
		//},
		{
		    field: 'IsShow_Sel', title: '状态', minWidth: 120, templet: function (d) {
		        var Show = d.IsShow_Sel;
		        var str = "";
		        if (Show == 0) {
		            str = "<span style='color:#5FB878;font-weight:bold'>启用</span>";
		            if (d.SaleEndTime != null) {
		                var begdate = new Date(d.SaleBegTime);
		                var enddate = new Date(d.SaleEndTime);
		                var strbeg = encodeURI(d.SaleBegTime);
		                var strend = encodeURI(d.SaleEndTime);
		                if (begdate > nowdate) {
		                    str += "<i class=\"layui-icon icon1\" id='saletime" + d.Code + "' title='商品未到销售时间' onclick=\"LookSaleTime('" + strbeg + "', '"+ strend +"', '" + d.Code + "')\">&#xe60b;</i>";
		                } else if (enddate < nowdate) {
		                    str += "<i class=\"layui-icon icon1\" id='saletime" + d.Code + "' title='商品已超过销售时间' onclick=\"LookSaleTime('" + strbeg + "', '" + strend + "', '" + d.Code + "')\">&#xe60b;</i>";
		                }
		            }
		        } else {
		            str = "<span style='color:#c2c2c2;font-weight:bold'>停用</span>";
		        }
		        var Audit = d.AuditState;
		        var str1 = "";
		        if (Audit == 0) {
		            str1 = "<span style='color:#01AAED;font-weight:bold'>待审核</span>";
		        } else if (Audit == 1) {
		            str1 = "<span style='color:#5FB878;font-weight:bold'>通过</span>";
		        } else {
		            str1 = "<span style='color:#FF5722;font-weight:bold'>不通过</span>";
		        }
		        var html = "状态：" + str;
		        html += "<br />审核：" + str1;
		        return html;
		    }
		},                             
		//{
		//    field: 'SupplierID_SelSourceName', title: '供应商', minWidth: 180, templet: function (d) {
		//        var html = "";
		//        html += "<span style='color:black;'>" + d.SupplierLevel + "</span>";
		//        html += "<span style='color:black;'> " + d.SupplierArea + "</span>";
		//        html += "<span> " + d.SupplierID_SelSourceName + "</span>";
		//        html += "<br /><span class='fontview2' lay-event=\"storeGood\" title='点击查看选品详情'><span style='color:#FF5722;font-weight:bold;font-size:1em'>" + d.StoreGoodNum +
  //                  "</span><span style='font-size:10px;'> 家店铺已选品<span/></span>";
		//        return html;
		//    }
		//},
      {
          title: '操作', width: 80, fixed: 'right', align: 'center', templet: function (d) {
              var strRtn = "";
              if (varButtonRight.indexOf('edit') >= 0) {
                  strRtn += "<a class=\"layui-btn layui-btn-normal layui-btn-xs\" lay-event=\"edit\">编辑</a> ";
              }
              return strRtn;
          }
      } 
    ]]
    , page: true
    , limit: 10
    , limits: [10, 15, 20, 25, 30]
    , text: {
        none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增 text: '对不起，加载出现异常！'
    }
    });

    //监听工具条
    table.on('tool(LAY-app-content-list)', function (obj) {
        //debugger;
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('确定删除吗？', function (index) {
                admin.req({
                    url: layui.setter.APIUrl + '/shop/goods_baseMgr/BatchDelete' //实际使用请改成服务端真实接口
                  , data: { "ids": data.PID }
                  , type: "post"
                    //, success: function (res) {
                    //}
                  , done: function (res) {
                      layer.close(index); //执行关闭 
                      table.reload('LAY-app-content-list');
                      layer.msg('删除成功');
                  }
                });
            });
        }
        else if (obj.event === 'edit') {
            //location.hash = '/shop/shop_goods_base/addChild/id=' + data.PID;
            admin.popup({
                title: '编辑信息'
                , area: ['80%', '96%']
                , zIndex: 1002
              , id: 'LAY-popup-content-edit'
              , success: function (layero, index) {
                  view(this.id).render('/shop/shop_goods_base/add', data).done(function () {
                      form.render(null, 'layuiadmin-app-form-list');
                      
                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function (data) {
                          debugger
                          var field = data.field; //获取提交的字段 
                          if (field.Images == "" || field.Images == null || field.Images == "[]") {
                              layer.msg("请上传商品介绍图！");
                              return false;
                          }
                          //if (field.IsShipping_Sel == 0  && (field.FreightID == "" || field.FreightID == null)) {
                          //    layer.msg("请选择运费模板！");
                          //    return false;
                          //}
                          //field["MemberPrice"] = GetMemberPrice();
                          var lindex = layer.load(1);
                          //提交 Ajax 成功后，关闭当前弹层并重载表格 
                          admin.req({
                              url: layui.setter.APIUrl + '/shop/goods_baseMgr/update' //实际使用请改成服务端真实接口
                          , data: field
                          , type: "post"
                          , done: function (res) {
                              layer.close(lindex); //执行关闭
                              layui.table.reload('LAY-app-content-list'); //重载表格
                              layer.close(index); //执行关闭
                              layer.msg('修改成功');
                          }
                          });
                      });
                  });
              }
            });
        }
        else if (obj.event === 'view') {
            admin.popup({
                title: '查看信息'
                , area: ['80%', '96%']
             , id: 'LAY-popup-content-view'
             , success: function (layero, index) {
                 view(this.id).render('/shop/shop_goods_base/view', data).done(function () {
                     form.render(null, 'layuiadmin-app-form-list');
                     //监听提交
                     form.on('submit(layuiadmin-app-form-submit)', function (data) {
                         layer.close(index); //执行关闭
                     });
                 });
             }
            });
        }
        else if (obj.event === 'storeGood') {
            GoodID = data.PID;
            if (varButtonRight.indexOf('storegood') >= 0) {
                StoreGoodRight = "1";
            }
            admin.popup({
                title: '已选品店铺'
                , area: ['76%', '90%']
             , id: 'LAY-popup-content-storeGood'
             , success: function (layero, index) {
                 view(this.id).render('/shop/shop_goods/indexBase', data).done(function () {
                     form.render(null, 'layuiadmin-app-form-list2');
                 });
             }
            });
        }
    });
    
    exports('shop_goods_base', {})

});

function LookMemberPrice(strEnCode, code) {
    var str = decodeURI(strEnCode);
    var Prices = JSON.parse(str);
    var content = "";
    layui.$.each(Prices, function (i, d) {
        if (content != "") {
            content += "<br />";
        }
        content += d.LevelStr + "：￥" + d.Price;
    })
    layer.tips(content, '#jg' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
function LookLimitArea(strEnCode, code) {
    var str = decodeURI(strEnCode);
    var content = "限购区域：<br />" + str;
    layer.tips(content, '#area' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
function LookSaleTime(strEnCode1, strEnCode2, code) {
    var str1 = decodeURI(strEnCode1);
    var str2 = decodeURI(strEnCode2);
    var content = "销售时间：<br />" + str1 + "<br />" + str2;
    layer.tips(content, '#saletime' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
function LookSyncNote(strEnCode, code) {
    var content = "自动同步店铺商品失败：<br />" + strEnCode + "<br />（可点击同步按钮手动同步）";
    layer.tips(content, '#syncnote' + code, {
        tips: [1, '#5FB878'],
        time: 4000
    });
}
